package com.etcsoft.examples.snippet;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Snippet005TreeCustomMenu {
    private class MyContentProvider implements ITreeContentProvider {

        @Override
        public Object[] getElements(Object inputElement) {
            return ((MyModel) inputElement).child.toArray();
        }

        @Override
        public void dispose() {

        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

        }

        @Override
        public Object[] getChildren(Object parentElement) {
            return this.getElements(parentElement);
        }

        @Override
        public Object getParent(Object element) {
            if (element == null) {
                return null;
            }

            return ((MyModel) element).parent;
        }

        @Override
        public boolean hasChildren(Object element) {
            return ((MyModel) element).child.size() > 0;
        }

    }

    public class MyModel {
        public MyModel parent;

        public List<MyModel> child = new ArrayList<MyModel>();

        public int counter;

        public MyModel(int counter, MyModel parent) {
            this.parent = parent;
            this.counter = counter;
        }

        @Override
        public String toString() {
            String rv = "Item ";
            if (this.parent != null) {
                rv = this.parent.toString() + ".";
            }

            rv += this.counter;

            return rv;
        }
    }

    public Snippet005TreeCustomMenu(Shell shell) {
        final TreeViewer v = new TreeViewer(shell);
        v.setLabelProvider(new LabelProvider());
        v.setContentProvider(new MyContentProvider());
        v.setInput(this.createModel());

        final Action a = new Action("") {
        };
        final MenuManager mgr = new MenuManager();
        mgr.setRemoveAllWhenShown(true);

        mgr.addMenuListener(new IMenuListener() {

            @Override
            public void menuAboutToShow(IMenuManager manager) {
                IStructuredSelection selection = (IStructuredSelection) v.getSelection();
                if (!selection.isEmpty()) {
                    a.setText("Action for "
                            + ((MyModel) selection.getFirstElement())
                                    .toString());
                    mgr.add(a);
                }
            }
        });
        v.getControl().setMenu(mgr.createContextMenu(v.getControl()));
    }

    private MyModel createModel() {

        MyModel root = new MyModel(0, null);
        root.counter = 0;

        MyModel tmp;
        for (int i = 1; i < 10; i++) {
            tmp = new MyModel(i, root);
            root.child.add(tmp);
            for (int j = 1; j < i; j++) {
                tmp.child.add(new MyModel(j, tmp));
            }
        }

        return root;
    }

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        new Snippet005TreeCustomMenu(shell);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }
}
